package cn.zucc.edu.madq.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.zucc.edu.madq.service.ClubService;
import cn.zucc.edu.madq.service.ManagerClubService;

import cn.zucc.edu.madq.pojo.ManagerClub;
import cn.zucc.edu.madq.pojo.User;

@Controller
@RequestMapping("managerClub")
public class ManagerClubController {

	@Autowired
	private ManagerClubService managerClubService;
	@Autowired
	private ClubService clubService;
	private static Logger logger = Logger
			.getLogger(ManagerClubController.class);

	@RequestMapping("delManagerClub")
	@ResponseBody
	public JSONPObject delManagerClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("managerClubId") int managerClubId,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		// User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			count = managerClubService.delMCById(managerClubId);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			System.out.println(count);
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			// throw e;
			System.out.println(count + "aaa");
			return new JSONPObject(jsonpCallback, count);
		}
	}

	@RequestMapping("updatManagerUser")
	@ResponseBody
	public JSONPObject updateClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("managerUserId") int managerUserId,
			@RequestParam("clubId") int clubId, HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			if (user.getUserId() == clubService.getClubById(clubId)
					.getClubBoss()) {
				ManagerClub mc = new ManagerClub();
				mc.setUserId(managerUserId);
				mc.setClubId(clubId);
				List<ManagerClub> listMC = managerClubService.select(mc);
				if (listMC.size() < 1) {
					count = managerClubService.insert(mc);
				} else {
					count = -2;
				}
			}
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}

	}
}
